package cn.sciento.farm.devicemanager.gateway.model.downlink.fertilizer;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.gateway.model.downlink.AbstractBlockData;
import cn.sciento.farm.devicemanager.gateway.model.downlink.MqttContent;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = false)
public class FertilizerConfigData  extends AbstractBlockData {

    private String eui;

    private Integer modbusAddr = 1;

    private Integer period;

    private Integer wfmRefresh;

    @Override
    public String generateId() {
        return String.format(MqttConstant.FERTILIZER_CONFIG_BLOCK_ID, this.eui);
    }

    @Override
    public String generateStatus() {
        return null;
    }

    @Override
    public MqttContent generate() {
        String topic = String.format(MqttConstant.FERTILIZER_DOWN_TOPIC, eui);
        Map<String,Object> map = new HashMap<>();
        if(period != null){
            map.put("wfmPeriod",this.period);
        }
        if(wfmRefresh != null){
            map.put("wfmRefresh",this.wfmRefresh);
        }
        map.put("id",modbusAddr);
        String payload = JSON.toJSONString(map);
        return new MqttContent(topic,payload);
    }
}
